﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Gw2Api
{
    public class WvWMatch
    {
        public enum WvWTeam
        {
            red,
            blue,
            green
        }

        public string id;
        public Dictionary<WvWTeam, int> scores;
        public Dictionary<WvWTeam, int> worlds;
        public Dictionary<WvWTeam, int[]> all_worlds;
        public class WvWMatchMap
        {
            public int id;
            public WvWMaps type;
            public Dictionary<WvWTeam, int> scores;
            public class WvWMatchObjective
            {
                public string id;
                public string type;
                public WvWFaction owner;
                public string claimed_by;
                public string owner_guild_name;
                public DateTime last_flipped;
                public int points_tick;
                public int points_capture;
                public int yaks_delivered;
                public int[] guild_upgrades;

                public string GuildName
                {
                    get
                    {
                        if (!string.IsNullOrEmpty(this.claimed_by) && string.IsNullOrEmpty(this.owner_guild_name))
                        {
                            GuildDetails guild = Gw2Api.GuildDetailsApi.GetGuildDetails(claimed_by);
                            this.owner_guild_name = guild.ToString();
                            return this.owner_guild_name;
                        }
                        else if (!string.IsNullOrEmpty(this.owner_guild_name))
                        {
                            return this.owner_guild_name;
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
                public WvWFaction faction
                {
                    get
                    {
                        return owner;
                    }
                }
                public WvWObjectiveDiff Diff(string matchupId, WvWMatchObjective otherObjective)
                {
                    Debug.Assert(otherObjective != null || otherObjective.id == this.id);
                    if (otherObjective != null && otherObjective.owner != this.owner)
                    {
                        return new WvWObjectiveDiff(matchupId, this.id, otherObjective.owner.ToString(), this.owner.ToString());
                    }
                    return null;
                }
            }
            public List<WvWMatchObjective> objectives;
            public List<WvWObjectiveDiff> Diff(string matchupId, WvWMatchMap otherMap)
            {
                List<WvWObjectiveDiff> diffs = new List<WvWObjectiveDiff>();
                foreach (var obj in objectives)
                {
                    WvWObjectiveDiff diff = obj.Diff(matchupId, otherMap.objectives.FirstOrDefault(o => o.id == obj.id));
                    if (null != diff)
                    {
                        diffs.Add(diff);
                    }
                }
                return diffs;
            }
        }
        public List<WvWMatchMap> maps;
        public List<WvWObjectiveDiff> Diff(WvWMatch otherDetail)
        {
            List<WvWObjectiveDiff> diffs = null;
            if (maps.Count == otherDetail.maps.Count)
            {
                diffs = new List<WvWObjectiveDiff>();
                foreach (var map in maps)
                {
                    List<WvWObjectiveDiff> mapDiff = map.Diff(this.id, otherDetail.maps.Where(m => m.type == map.type).First());
                    diffs = diffs.Concat(mapDiff).ToList();
                }

            }
            return diffs;
        }
    }

    class MatchApi
    {
    }
}
